#import "RSTAPIClient.h"

#import "AFNetworking.h"
#import "JSONResponseSerializerWithData.h"
#import "NSError+JSONSerializer.h"

#import "DataUtils.h"
//#import "PLTAppConstants.h"
#import "UserPreferences.h"

#define API_HOST @"http://123.56.91.23:8080"

@implementation RSTAPIClient

+ (instancetype)sharedClient {
    static RSTAPIClient *client = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        client = [[self alloc] initWithBaseURL:[NSURL URLWithString:API_HOST]];
//        NSString *token = [UserPreferences rstToken];
//        if (token.length > 0) {
//            [client setAuthorizationHeaderWithToken:token];
//        }

    });
    return client;
}

- (instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration {
    if (self = [super initWithBaseURL:url sessionConfiguration:configuration]) {
        self.requestSerializer = [AFHTTPRequestSerializer serializer];
        self.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

        self.responseSerializer = [JSONResponseSerializerWithData serializer];
        self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", nil];
        
        self.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        self.securityPolicy.allowInvalidCertificates = YES;

        [self.requestSerializer setValue:[DataUtils defaultUserAgentString] forHTTPHeaderField:@"User-Agent"];
    }
    return self;
}

#pragma mark - Utils
- (void)setAuthorizationHeaderWithToken:(NSString *)token {
    [self.requestSerializer setValue:token forHTTPHeaderField:@"X-Access-Token"];
}

+ (void)registerAccount:(NSString*)NickName
               passWord:(NSString*)Password
                 school:(NSUInteger)School
            phoneNumber:(NSString*)PhoneNumber
               favorite:(NSInteger)Favourite
                success:(void (^)(id data))success
                failure:(void (^)(id data))failure {
    [[RSTAPIClient sharedClient] POST:@"/Regist"
                           parameters:@{@"NickName": NickName, @"PhoneNumber": PhoneNumber, @"Password": Password, @"School": @(School), @"Favourite": @(Favourite)}
                              success:^(NSURLSessionDataTask *task, id JSON) {
                                  if (success) {
                                      dispatch_async(dispatch_get_main_queue(), ^{
                                          success(JSON);
                                      });
                                  }
                              }
                              failure:^(NSURLSessionDataTask *task, NSError *error) {
                                  if (failure) {
                                      dispatch_async(dispatch_get_main_queue(), ^{
                                          failure(error.customErrorData);
                                      });
                                  }
                              }];
}

+ (void)loginAccount:(NSString*)phoneNumber
            passWord:(NSString*)passWord
             success:(void (^)(id data))success
             failure:(void (^)(id data))failure {
    [[RSTAPIClient sharedClient] POST:@"PORT/Login"
                           parameters:@{@"PhoneNumber": phoneNumber, @"Password":passWord}
                              success:^(NSURLSessionDataTask *task, id JSON) {
                                  if (success) {
                                      dispatch_async(dispatch_get_main_queue(), ^{
                                          success(JSON);
                                      });
                                  }
                              }
                              failure:^(NSURLSessionDataTask *task, NSError *error) {
                                  if (failure) {
                                      dispatch_async(dispatch_get_main_queue(), ^{
                                          failure(error.customErrorData);
                                      });
                                  }
                              }];
}
//
//
//+ (NSString *)getSignature:(NSString *)identity provider:(NSString*)provider {
//    NSString *code = [NSString stringWithFormat:@"%@$%@$%@$%@", identity, provider, kApi3_AppKey, kApi3_AppSecret];
//    return [DataUtils md5HexDigest:code];
//}
//
+ (void)getSelfProfile:(void (^)(id data))success
               failure:(void (^)(id data))failure {
    [[RSTAPIClient sharedClient] POST:@"api/PORT/GetGeneralInfo"
                          parameters:nil
                             success:^(NSURLSessionDataTask *task, id data) {
                                 if (success) {
                                     dispatch_async(dispatch_get_main_queue(), ^{
                                         success(data);
                                     });
                                 }
                             }
                             failure:^(NSURLSessionDataTask *task, NSError *error) {
                                 if (failure) {
                                     dispatch_async(dispatch_get_main_queue(), ^{
                                         failure(error.customErrorData);
                                     });
                                 }
                             }];
}
//
//+ (void)checkAppNewVersion:(NSString *)version
//                   success:(void (^)(id))success
//                   failure:(void (^)(id))failure {
//    [[RSTAPIClient sharedClient] GET:@"/api/version"
//                          parameters:@{@"version": version, @"os": @"ios"}
//                             success:^(NSURLSessionDataTask *task, id data) {
//                                 if (success) {
//                                     dispatch_async(dispatch_get_main_queue(), ^{
//                                         success(data);
//                                     });
//                                 }
//                             }
//                             failure:^(NSURLSessionDataTask *task, NSError *error) {
//                                 if (failure) {
//                                     dispatch_async(dispatch_get_main_queue(), ^{
//                                         failure(error.customErrorData);
//                                     });
//                                 }
//                             }
//     ];
//}
//
//+ (void)bindingAccount:(NSString*)email
//                success:(void (^)(id data))success
//                failure:(void (^)(id data))failure {
//    [[RSTAPIClient sharedClient] PUT:@"/api/users/self/email"
//                           parameters:@{@"email": email}
//                              success:^(NSURLSessionDataTask *task, id JSON) {
//                                  if (success) {
//                                      dispatch_async(dispatch_get_main_queue(), ^{
//                                          success(JSON);
//                                      });
//                                  }
//                              }
//                              failure:^(NSURLSessionDataTask *task, NSError *error) {
//                                  if (failure) {
//                                      dispatch_async(dispatch_get_main_queue(), ^{
//                                          failure(error.customErrorData);
//                                      });
//                                  }
//                              }];
//}
//
//+ (void)verifyEmail:(NSString*)email
//               code:(NSString*)code
//            success:(void (^)(id data))success
//            failure:(void (^)(id data))failure {
//    [[RSTAPIClient sharedClient] POST:@"/api/users/self/email"
//                          parameters:@{@"email": email, @"code": code}
//                             success:^(NSURLSessionDataTask *task, id JSON) {
//                                 if (success) {
//                                     dispatch_async(dispatch_get_main_queue(), ^{
//                                         success(JSON);
//                                     });
//                                 }
//                             }
//                             failure:^(NSURLSessionDataTask *task, NSError *error) {
//                                 if (failure) {
//                                     dispatch_async(dispatch_get_main_queue(), ^{
//                                         failure(error.customErrorData);
//                                     });
//                                 }
//                             }];
//}
//
//+ (void)changeScreenName:(NSString*)username
//            success:(void (^)(id data))success
//            failure:(void (^)(id data))failure {
//    [[RSTAPIClient sharedClient] PATCH:@"/api/users/self/account"
//                            parameters:@{@"username": username}
//                               success:^(NSURLSessionDataTask *task, id JSON) {
//                                   if (success) {
//                                       dispatch_async(dispatch_get_main_queue(), ^{
//                                           success(JSON);
//                                       });
//                                   }
//                               }
//                               failure:^(NSURLSessionDataTask *task, NSError *error){
//                                   if (failure) {
//                                       dispatch_async(dispatch_get_main_queue(), ^{
//                                           failure(error.customErrorData);
//                                       });
//                                   }
//                               }];
//}
//
//+ (void)survive:(void (^)(id data))success
//        failure:(void (^)(id data))failure {
//    [[RSTAPIClient sharedClient] POST:@"/api/purge/survive"
//                           parameters:nil
//                              success:^(NSURLSessionDataTask *task, id JSON) {
//                                  if (success) {
//                                      dispatch_async(dispatch_get_main_queue(), ^{
//                                          success(JSON);
//                                      });
//                                  }
//                              }
//                              failure:^(NSURLSessionDataTask *task, NSError *error) {
//                                  if (failure) {
//                                      dispatch_async(dispatch_get_main_queue(), ^{
//                                          failure(error.customErrorData);
//                                      });
//                                  }
//                              }];
//}
//
//+ (void)getPurgeCountdown:(void (^)(id data))success
//                  failure:(void (^)(id data))failure {
//    [[RSTAPIClient sharedClient] GET:@"/api/purge/countdown"
//                          parameters:nil
//                             success:^(NSURLSessionDataTask *task, id data) {
//                                 if (success) {
//                                     dispatch_async(dispatch_get_main_queue(), ^{
//                                         success(data);
//                                     });
//                                 }
//                             }
//                             failure:^(NSURLSessionDataTask *task, NSError *error) {
//                                 if (failure) {
//                                     dispatch_async(dispatch_get_main_queue(), ^{
//                                         failure(error.customErrorData);
//                                     });
//                                 }
//                             }];
//}
//
//+ (void)emailLogin:(NSString*)email
//               success:(void (^)(id data))success
//               failure:(void (^)(id data))failure {
//    [[RSTAPIClient sharedClient] PUT:@"/api/auth/email"
//                          parameters:@{@"email": email}
//                             success:^(NSURLSessionDataTask *task, id JSON) {
//                                 if (success) {
//                                     dispatch_async(dispatch_get_main_queue(), ^{
//                                         success(JSON);
//                                     });
//                                 }
//                             }
//                             failure:^(NSURLSessionDataTask *task, NSError *error) {
//                                 if (failure) {
//                                     dispatch_async(dispatch_get_main_queue(), ^{
//                                         failure(error.customErrorData);
//                                     });
//                                 }
//                             }];
//}
//
+ (void)verifyEmailLogin:(NSString*)email
                    code:(NSString*)code
                deviceId:(NSString*)deviceId
                 success:(void (^)(id data))success
                 failure:(void (^)(id data))failure {
    [[RSTAPIClient sharedClient] POST:@"api/PORT/CreateActivity"
                           parameters:@{@"email": email, @"code": code, @"device_id": deviceId}
                              success:^(NSURLSessionDataTask *task, id JSON) {
                                  if (success) {
                                      dispatch_async(dispatch_get_main_queue(), ^{
                                          success(JSON);
                                      });
                                  }
                              }
                              failure:^(NSURLSessionDataTask *task, NSError *error) {
                                  if (failure) {
                                      dispatch_async(dispatch_get_main_queue(), ^{
                                          failure(error.customErrorData);
                                      });
                                  }
                              }];
}

@end
